package org.viva.qq;

import org.viva.api.HTTPUtils;

import net.sf.json.JSONObject;

public class ConnectQQ {

    public final static String QQ_LOGIN      = "https://graph.qq.com/oauth2.0/authorize?response_type=code&client_id=APPID&redirect_uri=REDIRECT_URI&scope=SCOPE";
    public final static String TOKEN_URL     = "https://graph.qq.com/oauth2.0/token?grant_type=authorization_code&client_id=APPID&client_secret=APPKEY&code=CODE&state=&redirect_uri=REDIRECT_URI";
    public final static String ME_URL        = "https://graph.qq.com/oauth2.0/me?access_token=TOKEN";
    public final static String USER_INFO_URL = "https://graph.qq.com/user/get_user_info?access_token=TOKEN&oauth_consumer_key=APPID&openid=OPENID";

    public static String getLoginUrl(String appid, String scope, String redirect_uri) {
        String url = QQ_LOGIN;
        try {
            url = url.replace("APPID", appid).replace("SCOPE", scope).replace("REDIRECT_URI", redirect_uri);
        } catch (Exception e) {
        }
        return url;
    }

    public static String getToken(String appid, String appkey, String code, String redirect_uri) {
        String url = TOKEN_URL;
        url = url.replace("APPID", appid).replace("APPKEY", appkey).replace("CODE", code).replace("REDIRECT_URI", redirect_uri);
        String token = HTTPUtils.doGet(url);
        System.out.println(token);
        String[] ss = token.split("[&]");
        for (String s : ss) {
            String[] kv = s.split("[=]");
            if ("access_token".equals(kv[0])) {
                return kv[1];
            }
        }
        return token;
    }

    public static String getMe(String token) {
        String url = ME_URL;
        url = url.replace("TOKEN", token);
        String me = HTTPUtils.doGet(url);
        me = me.substring(me.indexOf("(") + 1, me.indexOf(")") - 1);
        System.out.println(me);
        return JSONObject.fromObject(me).getString("openid");
    }

    public static String getUserInfo(String token, String appid, String openid) {
        String url = USER_INFO_URL;
        url = url.replace("APPID", appid).replace("TOKEN", token).replace("OPENID", openid);
        String info = HTTPUtils.doGet(url);
        System.out.println(info);
        return info;
    }

    public static void main(String[] args) {// 4CC2CAC99ED327A9C513DDF2DFE83A79
        String code = "1B59EB5066AB64A9AF1CD3AC3A624984";
        String appid = "101173979";
        String appkey = "568efad54c8f9170f097c975807a1756";
        String redirect_uri = "http://www.dowdybear.com/login/qq";
        String token = getToken(appid, appkey, code, redirect_uri);
        String openid = getMe(token);
        getUserInfo(token, appid, openid);
    }

    // http://www.dowdybear.com/login/qq?#access_token=E3A8E400B28F4EF4A9BFE27512B819CA&expires_in=7776000
    // https://graph.qq.com/user/get_user_info?access_token=E3A8E400B28F4EF4A9BFE27512B819CA&oauth_consumer_key=101173979&openid=4CC2CAC99ED327A9C513DDF2DFE83A79
    // https://graph.qq.com/oauth2.0/authorize?response_type=code&client_id=101173979&redirect_uri=http://www.dowdybear.com/login/qq&scope=邋遢熊
    // https://graph.qq.com/oauth2.0/token?grant_type=authorization_code&client_id=101173979&client_secret=568efad54c8f9170f097c975807a1756&code=66F5E2C54584FCEADE6C834A68E9A016&state=&redirect_uri=http://www.dowdybear.com/login/qq
    // https://graph.qq.com/oauth2.0/me?access_token=E3A8E400B28F4EF4D6B88AA313B38CC7
    // https://graph.qq.com/user/get_user_info?access_token=E3A8E400B28F4EF4D6B88AA313B38CC7&oauth_consumer_key=101173979&openid=4CC2CAC99ED327A9C513DDF2DFE83A79

}
